// index.js
// 获取应用实例
const db = wx.cloud.database().collection('User')
Page({
  data: {
    // 首页展示科目信息
    subjectList: [{
        "subjectName": '数学',
        "bg_img": "cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/数学.png",
        "sub_img": "cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/sub数学.png"

      }, {
        "subjectName": '物理',
        "sub_img": "cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/sub物理.png",
        "bg_img": 'cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/物理.png'

      }, {
        "subjectName": '化学',
        "sub_img": "cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/sub化学.png",
        "bg_img": 'cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/化学.png'

      }, {
        "subjectName": '英语',
        "sub_img": "cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/sub英语.png",
        "bg_img": 'cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/英语.png'

      },
      {
        "subjectName": '力学',
        "sub_img": "cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/sub力学.png",
        "bg_img": 'cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/力学.png'

      },
      {
        "subjectName": '计算机',
        "sub_img": "cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/sub计算机.png",
        "bg_img": 'cloud://zhixin-cloud-8gmkon9vea6aff6a.7a68-zhixin-cloud-8gmkon9vea6aff6a-1307526469/images/计算机.png'
      }
    ],
  },
  //点击某科目，进入展示页面
  click_subject: async function (e) {
    if (this.data.info.length == 0) {
      wx.showToast({
        icon: 'error',
        title: '请先实名认证',
      })
    } else {
      var index = e.currentTarget.dataset.index;
      var name = this.data.subjectList[index].subjectName;
      var img = this.data.subjectList[index].sub_img;
      wx.navigateTo({
        url: '../sub_specific/sub_specific?selected_subname=' + name + '&selected_subimg=' + img
      })
    }

  },

  onLoad() {
    wx.showLoading({
      title: '请稍等',
      mask: true
    })
    this.getOpenid()
  },

  // 获取用户openid
  getOpenid() {
    let that = this;
    wx.cloud.callFunction({
      name: 'getOpenid',
      complete: res => {
        console.log('云函数获取到的openid: ', res.result.openId)
        var openid = res.result.openId;
        that.setData({
          openid: openid
        })
        this.refresh()
      }
    })
  },

  refresh: function () {

    db.where({
      _openid: this.data.openid
    }).get().then(res => {
      this.setData({
        info: res.data,
      })
    }).then(res => {
      wx.hideLoading()
    })
  },

  onShow() {
    this.getOpenid()
  }
})